Sugar - Photosensitive
Introduction
Detect the ambient light intensity and return the analog data.
Parameters
Dimension | 24 x 24 x 16 mm |
---|---|
Weight | 4.7 g |
Signal | Analog input |
Range | 0~3.3V correspond to future board 0~4095 |
Using on micro:bit
Coding Platform
Microsoft MakeCode for micro:bit Use Makecode as coding platform
Add Sugar Plugin
Search Sugar in extensions, click add
Building Blocks - Function Description
Serial Number | Building Block Image | Building Block Function |
---|---|---|
1 | Get the numerical value of the light-sensitive sensor (0-1023) |
Circuit Connection
Microbit Interface | Wiring | |||
---|---|---|---|---|
Light Sensor | P0 | White PH2.0-3Pin Interface Wire |
Coding
Case: Determine Light Intensity
Using Kittenblock
Connect the light sensor to the P1 of Robotbit Edu using a 3PIN terminal wire. Turn on the Robotbit power supply to see the red light at the bottom of the light sensor module light up (indicating that the module is powered normally)
The brightness of the future board screen is filled with the screen, and the filled value is the lightness value 0~255(from black to white). If the ambient light value 0~4095 is to correspond to the screen brightness value 0~255, the mapping block needs to be used to correspond the two intervals one by one. Choose to run the program in the online running or offline uploading mode to see the effect
If you are not sure how to run online or upload offline, please refer to Quick Start
Ambient Light Value
value()
- Return value: 0~4095
from future import *
from sugar import *
light = Light('P1')
Interval mapping formula, no need to understand too much, just use it directly
def valmap(x, in_min, in_max, out_min, out_max): return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min)
Turn off synchronous refresh for smoother screen
screen.sync = 0 while True: x = light.value() screen.fill(round(valmap(x, 0, 4096, 0, 255))) screen.refresh()
Effect Demonstration
Realize the brightness of the ambient light, and the brightness of the face board screen will change accordingly.